package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.constant.OrderFeaturesKey;
import com.alibaba.citrus.cr.common.constant.SaleOrderConstants;
import com.alibaba.citrus.cr.common.enums.GeneralStatusEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.OrderCategoryEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.OrderChannelEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.SalesOrderReasonEnum;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.SetOrderTag;
import com.alibaba.citrus.cr.mn.order.center.common.util.MoneyUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.crb2btradecenter.model.dto.PageQuerySaleOrderDetailGroupRequest;
import com.epoch.app.crb2btradecenter.obtcsaleorderindex.model.ObtcSaleOrderIndex;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.model.ObtcSaleOrderLineIndex;
import com.epoch.app.mnordercenter.model.dto.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName AtomOrderDOToSalesOrderAtomQueryResponseConvertor
 * @description: TODO
 * @date 2023/04/27 13:28
 * @version: 1.0
 */
public class AtomOrderDOToReturnOrderAtomQueryResponseConvertor {

    public static List<ReturnOrderAtomQueryResponse> convert(List<AtomOrderDO> atomOrderDoList) {
        if(CollectionUtils.isEmpty(atomOrderDoList)) {
            return Lists.newArrayList();
        }
        List<ReturnOrderAtomQueryResponse> responseList = Lists.newArrayList();
        for (AtomOrderDO atomOrderDO : atomOrderDoList) {
            Order order = atomOrderDO.getOrder();
            List<OrderLine> orderLines = atomOrderDO.getOrderLines();

            ReturnOrderAtomQueryResponse orderResp = convertOrder(order);
            orderResp.setOrderDetails(convertOrderLine(orderLines, order));

            responseList.add(orderResp);
        }
        // 针对字段null处理为空字符串""，空List转为[]
        String jsonArrString = JSONArray.toJSONString(responseList, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
        responseList = JSONArray.parseArray(jsonArrString, ReturnOrderAtomQueryResponse.class);

        return responseList;
    }

    private static List<ReturnOrderAtomQueryDetailResponse> convertOrderLine(List<OrderLine> orderLines, Order order) {
        if (CollectionUtils.isEmpty(orderLines)) {
            return Lists.newArrayList();
        }
        List<ReturnOrderAtomQueryDetailResponse> responses = Lists.newArrayList();
        for (OrderLine orderLine : orderLines) {
            ReturnOrderAtomQueryDetailResponse response = ReturnOrderAtomQueryDetailResponse.builder()
                    .closeCode(orderLine.getRefuseReasonCode())
                    .closeMsg(orderLine.getRefuseReasonName())
                    .deliveryFactoryCode(orderLine.getDeliveryFactoryCode())
                    .deliveryFactoryName(orderLine.getDeliveryFactoryName())
                    .isClose(String.valueOf(orderLine.getClosed()))
                    .isCloseName(GeneralStatusEnum.descByCode(orderLine.getClosed()))
                    .lineNum(orderLine.getSerialNumber())
                    .note(orderLine.getRemark())
                    .outLineNum(orderLine.getOuterOrderLineId())
                    .sourceLineNum(orderLine.getSourceLineNum())
                    //.produceDate(formatDate(orderLine.getProductationTime()))
                    .productBarCode(orderLine.getProductBarCode())
                    .productCode(parseMoneyFromFeatures(orderLine, "productCode", orderLine.getScItemOutCode()))
                    .productName(orderLine.getScItemTitle())
                    .salesUnitCode(orderLine.getSaleUnit())
                    .salesUnitName(orderLine.getSaleUnitName())
                    .stockCode(orderLine.getStockCode())
                    .stockConversionNum(orderLine.getStockConversionNum())
                    .stockName(orderLine.getStockName())
                    .stockUnitCode(orderLine.getInventoryUnitCode())
                    .stockUnitName(orderLine.getInventoryUnit())
                    .totalAmount(orderLine.getShouldPayFee())
                    .unitConversionRelationship(orderLine.getUnitConvert())
                    .unitPrice(orderLine.getUnitPrice())
                    .cumulativePlanDeliveryQuantity(new BigDecimal(StringUtils.defaultString(orderLine.getCumulativeDeliveryQuantity(), "0")).longValue())
                    .cumulativeDeliveryQuantity(new BigDecimal(StringUtils.defaultString(orderLine.getDeliveryAmount(), "0")).longValue())
                    .productQuantity(new Long(orderLine.getQuantity()))
                    .manualPrice(parseFromFeatures(orderLine, SaleOrderConstants.MANUAL_PRICE, orderLine.getManualPrice()))
                    .manualTotalAmount(parseFromFeatures(orderLine, SaleOrderConstants.MANUAL_TOTAL_AMOUNT, orderLine.getManualTotalAmount()))
                    .status(orderLine.getStatus())
                    .innerProductCode(orderLine.getInnerProductCode())
                    .innerProductName(orderLine.getInnerProductName())
                    .shippingWarehouseCode(parseFromFeatures(orderLine, "logicWarehouseCode", orderLine.getShippingWarehouseCode()))
                    .shippingWarehouseName(order.getWareHouseName())
                    .bizFeatures(orderLine.getBizFeatures())
                    .productionDate(formatDate(orderLine.getProductationTime()))
                    .build();
            // 换算金额(分) -> (元)
            reSetMoneyForNYC(response);

            responses.add(response);
        }
        return responses;
    }

    public static QueryReturnResponse AtomOrderDOToQueryReturnResponseConvert(List<AtomOrderDO> result) {
        QueryReturnResponse response = new QueryReturnResponse();
        if (CollectionUtils.isEmpty(result)) return response;
        AtomOrderDO atomOrderDO = result.get(0);
        Order order = atomOrderDO.getOrder();
        List<OrderLine> orderLines = atomOrderDO.getOrderLines();

        response = QueryReturnResponse.builder()
                .fromSys(order.getFromSys())
                .businessType(String.valueOf(order.getBusinessType()))
                .businessTypeName(order.getBusinessTypeName())
                .businessUnitCode(order.getBisUnitOutCode())
                .businessUnitName(order.getBisUnitName())
                .orderCategoryCode(order.getOrderCategoryCode())
                .orderChannelCode(order.getOrderChannelCode())
                .salesOrderNo(order.getOrderBizId())
                .sourceOrderNo(order.getSourceOrderNo())
                .outOrderNo(order.getOuterOrderId())
                .originalOrderNo(order.getOriginalOrderNo())
                .productGroupCode(order.getProductGroupCode())
                .productGroupName(order.getProductGroupName())
                .businessChannel(order.getChannelCode())
                .businessChannelName(order.getChannelName())
                .salesOrganizationCode(order.getOrganizationOutCode())
                .salesOrganizationName(order.getOrganizationName())
                .customerCode(order.getCustomerCode())
                .customerName(order.getCustomerName())
                .shipToPartyCode(order.getReceiveAddressCode())
                .shipToPartyName(order.getReceiveDetailAddress())
                .salesChannelCode(order.getSaleChannelOutCode())
                .salesChannelName(order.getSaleChannelName())
                .salesDepartmentCode(order.getDepartmentOutCode())
                .salesDepartmentName(order.getDepartmentName())
                .customerGrade(order.getCustomerGrade())
                .customerGroup(order.getCustomerGroup())
                .customerGroupName(order.getCustomerGroupName())
                .salesOrderType(order.getOrderType())
                .salesOrderTypeName(order.getOrderTypeName())
                .deliveryMethodCode(order.getTransportModeCode())
                .deliveryMethodName(order.getTransportModeName())
                .expectedDeliveryDate(DateUtils.formatDate(order.getCustomerExpectDate(), DateUtils.DATE_FORMAT))
                .fulfillmentTypeCode(order.getFulfillmentTypeCode())
                .fulfillmentPriority(order.getFulfillmentPriority())
                .autoFulfillment(order.getAutoFulfillment())
                .orderReasonCode(order.getCreateReason())
                .orderReasonName(order.getCreateReasonName())
                .creatorCode(order.getCreatorCode())
                .creatorName(order.getCreatorName())
                .createTime(DateUtils.formatDate(order.getGmtCreate(), DateUtils.DATETIME_FORMAT))
                .modifierCode(order.getModifierCode())
                .modifierName(order.getModifierName())
                .modifierTime(DateUtils.formatDate(order.getGmtModified(), DateUtils.DATETIME_FORMAT))
                .customerPurchaseOrderNo(order.getCustomerReferenceNo())
                .customerPurchaseDate(DateUtils.formatDate(order.getCustomerPurchaseDate(), DateUtils.DATE_FORMAT))
                .note(order.getRemark())
                .orderStatus(String.valueOf(order.getStatus()))
                .bizFeatures(order.getBizFeatures())
                .costCenter(order.getCostCenter())
                .subStatus(order.getSubStatus())
                .orderDetails(orderLinesConvert(orderLines))
                .build();

        if (org.apache.commons.lang3.StringUtils.isNotBlank(response.getOrderCategoryCode())){
            response.setOrderCategoryName(OrderCategoryEnum.of(response.getOrderCategoryCode()).getDesc());
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(response.getOrderChannelCode())){
            response.setOrderChannelName(OrderChannelEnum.of(response.getOrderChannelCode()).getDesc());
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(order.getOrderBizTag())){
            response.setOrderBizTag(Arrays.asList(order.getOrderBizTag().split(",")));
        }

        // 针对字段null处理为空字符串""，空List转为[]
        String jsonString = JSONArray.toJSONString(response, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
        response = JSONArray.parseObject(jsonString, QueryReturnResponse.class);

        return response;
    }

    private static List<QueryReturnDetailResponseDTO> orderLinesConvert(List<OrderLine> orderLines) {
        List<QueryReturnDetailResponseDTO> detailList = new ArrayList<>();
        if (CollectionUtils.isEmpty(orderLines)) return detailList;

        for (OrderLine orderLine : orderLines) {
            QueryReturnDetailResponseDTO detailResponseDTO = new QueryReturnDetailResponseDTO();
            detailResponseDTO.setLineNum(orderLine.getSerialNumber());
            detailResponseDTO.setSourceLineNum(orderLine.getSourceLineNum());
            detailResponseDTO.setOutLineNum(orderLine.getOuterOrderLineId());
            detailResponseDTO.setInnerProductCode(orderLine.getInnerProductCode());
            detailResponseDTO.setInnerProductName(orderLine.getInnerProductName());
            detailResponseDTO.setProductBarCode(orderLine.getProductBarCode());
            detailResponseDTO.setProductCode(orderLine.getScItemOutCode());
            detailResponseDTO.setProductName(orderLine.getScItemTitle());
            detailResponseDTO.setSalesUnitCode(orderLine.getSaleUnit());
            detailResponseDTO.setSalesUnitName(orderLine.getSaleUnitName());
            detailResponseDTO.setStockUnitCode(orderLine.getInventoryUnitCode());
            detailResponseDTO.setStockUnitName(orderLine.getInventoryUnit());
            detailResponseDTO.setUnitConversionRelationship(orderLine.getUnitConvert());
            detailResponseDTO.setUnitPrice(Objects.isNull(orderLine.getUnitPrice()) ? "" : MoneyUtils.moneyFen2Yuan(Long.valueOf(nvlToEmptyStr(orderLine.getUnitPrice()))));
            detailResponseDTO.setTotalAmount(Objects.isNull(orderLine.getShouldPayFee()) ? "" : MoneyUtils.moneyFen2Yuan(Long.valueOf(nvlToEmptyStr(orderLine.getShouldPayFee()))));
            detailResponseDTO.setProductionDate(DateUtils.formatDate(orderLine.getProductationTime(), DateUtils.DATE_FORMAT));
            detailResponseDTO.setProductQuantity(String.valueOf(orderLine.getQuantity()));
            detailResponseDTO.setCumulativeNoticeQuantity(orderLine.getCumulativeNoticeQuantity());
            detailResponseDTO.setCumulativePlanDeliveryQuantity(orderLine.getCumulativeDeliveryQuantity());
            detailResponseDTO.setCumulativeDeliveryQuantity(orderLine.getDeliveryAmount());
            detailResponseDTO.setCumulativeWaitOutboundQuantity(orderLine.getCumulativeWaitOutboundQuantity());
            detailResponseDTO.setCumulativeReceivedQuantity(orderLine.getCumulativeReceivedQuantity());
            detailResponseDTO.setIsClose(String.valueOf(orderLine.getClosed()));
            detailResponseDTO.setCloseCode(orderLine.getRefuseReasonCode());
            detailResponseDTO.setCloseMsg(orderLine.getRefuseReasonName());
            detailResponseDTO.setNote(orderLine.getRemark());
            detailResponseDTO.setStatus(String.valueOf(orderLine.getStatus()));
            detailResponseDTO.setBizFeatures(orderLine.getBizFeatures());
            detailResponseDTO.setTxWarehouseCode(orderLine.getReceiveWarehouseOutCode());
            detailResponseDTO.setTxWarehouseName(orderLine.getReceiveWarehouseName());
            detailResponseDTO.setTxFactoryCode(orderLine.getReceiveFactoryCode());
            detailResponseDTO.setTxFactoryName(orderLine.getReceiveFactoryName());
            detailResponseDTO.setTxStockCode(orderLine.getReceiveStockCode());
            detailResponseDTO.setTxStockName(orderLine.getReceiveStockName());
            detailResponseDTO.setShippingWarehouseCode(orderLine.getShippingWarehouseOutCode());
            detailResponseDTO.setShippingWarehouseName(orderLine.getShippingWarehouseName());
            detailResponseDTO.setDeliveryFactoryCode(orderLine.getDeliveryFactoryCode());
            detailResponseDTO.setDeliveryFactoryName(orderLine.getDeliveryFactoryName());
            detailResponseDTO.setStockCode(orderLine.getStockCode());
            detailResponseDTO.setStockName(orderLine.getStockName());
            detailResponseDTO.setSubStatus(orderLine.getSubStatus());

            if (org.apache.commons.lang3.StringUtils.isNotBlank(orderLine.getFeatures())){
                Map<String, String> orderLineFeatures = JSONObject.parseObject(orderLine.getFeatures(),Map.class);
                String invoiceNo = orderLineFeatures.get(OrderFeaturesKey.INVOICE_NO);
                String invoiceLineNo = orderLineFeatures.get(OrderFeaturesKey.INVOICE_LINE_NO);
                detailResponseDTO.setInvoiceNo(invoiceNo);
                detailResponseDTO.setInvoiceLineNo(invoiceLineNo);
            }

            detailList.add(detailResponseDTO);
        }

        return detailList;
    }

    private static String parseMoneyFromFeatures(OrderLine orderLine, String key, String defaultVal) {
        Map<String, String> featuresMap = JSONObject.parseObject(orderLine.getFeatures(), new TypeReference<Map<String, String>>() {});
        if(MapUtils.isEmpty(featuresMap)) {
            return defaultVal;
        }
        return featuresMap.getOrDefault(key, defaultVal);
    }

    private static String parseFromFeatures(OrderLine orderLine, String key, String defaultVal) {
        Map<String, String> featuresMap = JSONObject.parseObject(orderLine.getFeatures(), new TypeReference<Map<String, String>>() {});
        if(MapUtils.isEmpty(featuresMap)) {
            return defaultVal;
        }
        return featuresMap.getOrDefault(key, defaultVal);
    }

    /**
     * 金额转换 分转元
     */
    private static void reSetMoneyForNYC(ReturnOrderAtomQueryDetailResponse response) {
        response.setUnitPrice(StringUtils.isBlank(response.getUnitPrice()) ? BigDecimal.ZERO.toPlainString() : new BigDecimal(response.getUnitPrice()).divide(new BigDecimal("100"),2, RoundingMode.DOWN).toPlainString());
        response.setTotalAmount(StringUtils.isBlank(response.getTotalAmount()) ? BigDecimal.ZERO.toPlainString() : new BigDecimal(response.getTotalAmount()).divide(new BigDecimal("100"),2, RoundingMode.DOWN).toPlainString());
        response.setManualPrice(StringUtils.isBlank(response.getManualPrice()) ? BigDecimal.ZERO.toPlainString() : new BigDecimal(response.getManualPrice()).divide(new BigDecimal("100"),2, RoundingMode.DOWN).toPlainString());
        response.setManualTotalAmount(StringUtils.isBlank(response.getManualTotalAmount()) ? BigDecimal.ZERO.toPlainString() : new BigDecimal(response.getManualTotalAmount()).divide(new BigDecimal("100"),2, RoundingMode.DOWN).toPlainString());
    }

    private static ReturnOrderAtomQueryResponse convertOrder(Order order) {
        ReturnOrderAtomQueryResponse response = ReturnOrderAtomQueryResponse.builder()
                .businessChannel(order.getChannelCode())
                .businessChannelName(order.getChannelName())
                .createTime(formatDate(order.getGmtCreate(), DateUtils.DATETIME_FORMAT))
                .creatorCode(parseUserInfoCode(order.getCreator()))
                .creatorName(order.getCreatorName())
                .customerCode(order.getCustomerCode())
                .customerName(order.getCustomerName())
                .customerPurchaseDate(formatDate(order.getCustomerPurchaseDate(), DateUtils.DATE_FORMAT))
                .customerPurchaseOrderNo(order.getCustomerReferenceNo())
                .deliveryMethodCode(order.getTransportModeCode())
                .deliveryMethodName(order.getTransportModeName())
                .expectedDeliveryDate(formatDate(order.getCustomerExpectDate(), DateUtils.DATE_FORMAT))
                .fromSys(order.getFromSys())
                .modifierCode(parseUserInfoCode(order.getModifier()))
                .modifierName(order.getModifierName())
                .modifierTime(formatDate(order.getGmtModified(), DateUtils.DATETIME_FORMAT))
                .note(order.getRemark())
                .orderReasonCode(order.getCreateReason())// TODO
                .orderReasonName(SalesOrderReasonEnum.descByCode(order.getCreateReason()))
                .orderStatus(String.valueOf(order.getStatus()))
                .outOrderNo(order.getOuterOrderId())
                .productGroupCode(order.getProductGroupCode())
                .productGroupName(order.getProductGroupName())
                .salesChannelCode(order.getSaleChannelOutCode())
                .salesChannelName(order.getSaleChannelName())
                .salesDepartmentCode(order.getDepartmentOutCode())
                .salesDepartmentName(order.getDepartmentName())
                .salesOrderNo(order.getOrderBizId())
                .salesOrderType(order.getOrderType())
                .salesOrderTypeName(order.getOrderTypeName())
                .salesOrganizationCode(order.getOrganizationOutCode())
                .salesOrganizationName(order.getOrganizationName())
                .shippingWarehouseCode(order.getWareHouseOutCode())
                .shippingWarehouseName(order.getWareHouseName())
                .shipToPartyCode(order.getReceiveAddressCode())
                .shipToPartyName(order.getReceiveDetailAddress())
                .sourceOrderNo(order.getSourceOrderNo())
                .businessUnitCode(order.getBisUnitOutCode())
                .businessUnitName(order.getBisUnitName())
                .businessType(order.getBusinessType())
                .bizFeatures(order.getBizFeatures())
                .originalOrderNo(order.getOriginalOrderNo())
                .orderCategoryCode(order.getOrderCategoryCode())
                .orderCategoryName(OrderCategoryEnum.descByCode(order.getOrderCategoryCode()))
                .orderChannelCode(order.getOrderChannelCode())
                .orderChannelName(OrderChannelEnum.descByCode(order.getOrderChannelCode()))
                .fulfillmentTypeCode(order.getFulfillmentTypeCode())
                .fulfillmentPriority(order.getFulfillmentPriority())
                .autoFulfillment(order.getAutoFulfillment())
                .orderBizTag(StringUtils.isNotBlank(order.getOrderBizTag()) ? Arrays.asList(order.getOrderBizTag().split(",")) : Lists.newArrayList())
                .creatorCode(order.getCreatorCode())
                .creatorName(order.getCreatorName2())
                .modifierCode(order.getModifierCode())
                .modifierName(order.getModifierName2())
                .build();

        return response;
    }

    private static String parseUserInfoCode(String dbCreator) {
        if(StringUtils.isBlank(dbCreator) || !JSONValidator.from(dbCreator).validate()) {
            return dbCreator;
        }
        Map<String, String> creatorInfo = JSONObject.parseObject(dbCreator, new TypeReference<Map<String, String>>() {});
        return creatorInfo.getOrDefault("empId", dbCreator);
    }

    private static String formatDate(Date dte, Object... pattern) {
        if(Objects.nonNull(dte)) {
            return DateUtils.formatDate(dte, pattern);
        }
        return null;
    }

    public static SalesOrderAtomPageQueryRequest convertPageQueryRequest(ReturnOrderGroupPageQueryRequest request) {
        SalesOrderAtomPageQueryRequest salesOrderAtomPageQueryRequest = new SalesOrderAtomPageQueryRequest();
        salesOrderAtomPageQueryRequest.setFromSysList(request.getFromSysList());
        salesOrderAtomPageQueryRequest.setBusinessType(request.getBusinessType());
        salesOrderAtomPageQueryRequest.setBusinessUnitCodeList(request.getBusinessUnitCodeList());
        salesOrderAtomPageQueryRequest.setPage(request.getPage());
        salesOrderAtomPageQueryRequest.setSize(request.getSize());
        salesOrderAtomPageQueryRequest.setOrderChannelCodeList(request.getOrderChannelCodeList());
        salesOrderAtomPageQueryRequest.setSalesOrderNoList(request.getSalesOrderNoList());
        salesOrderAtomPageQueryRequest.setOriginalOrderNoList(request.getOriginalOrderNoList());
        salesOrderAtomPageQueryRequest.setSourceOrderNoList(request.getSourceOrderNoList());
        if (CollectionUtils.isNotEmpty(request.getOutOrderNoList())){
            salesOrderAtomPageQueryRequest.setOutOrderNoList(request.getOutOrderNoList());
        }
        salesOrderAtomPageQueryRequest.setProductGroupCode(request.getProductGroupCode());
        salesOrderAtomPageQueryRequest.setBusinessChannelCode(request.getBusinessChannelCode());
        salesOrderAtomPageQueryRequest.setSalesOrganizationCode(request.getSalesOrganizationCode());
        salesOrderAtomPageQueryRequest.setCustomerCode(request.getCustomerCode());
        salesOrderAtomPageQueryRequest.setShipToPartyCode(request.getShipToPartyCode());
        salesOrderAtomPageQueryRequest.setSalesChannelCode(request.getSalesChannelCodeList());
        salesOrderAtomPageQueryRequest.setSalesDepartmentCode(request.getSalesDepartmentCodeList());
        salesOrderAtomPageQueryRequest.setCustomerGroup(request.getCustomerGroup());
        salesOrderAtomPageQueryRequest.setSalesOrderType(request.getSalesOrderTypeList());
        salesOrderAtomPageQueryRequest.setStatusList(request.getOrderStatusList());
        salesOrderAtomPageQueryRequest.setDeliveryMethodCodeList(request.getDeliveryMethodCodeList());
        salesOrderAtomPageQueryRequest.setExpectedDeliveryBeginDate(request.getExpectedDeliveryBeginDate());
        salesOrderAtomPageQueryRequest.setExpectedDeliveryEndDate(request.getExpectedDeliveryEndDate());
        salesOrderAtomPageQueryRequest.setFulfillmentTypeCode(request.getFulfillmentTypeCode());
        salesOrderAtomPageQueryRequest.setAutoFulfillment(request.getAutoFulfillment());
        salesOrderAtomPageQueryRequest.setFulfillmentPriority(request.getFulfillmentPriority());
        salesOrderAtomPageQueryRequest.setOrderBizTag(request.getOrderBizTag());
        salesOrderAtomPageQueryRequest.setOrderReasonCodeList(request.getOrderReasonCodeList());
        salesOrderAtomPageQueryRequest.setShippingWarehouseCodeList(request.getShippingWarehouseCodeList());
        salesOrderAtomPageQueryRequest.setReceiveWarehouseCodeList(request.getTxWarehouseCodeList());
        salesOrderAtomPageQueryRequest.setReceiveStockCode(request.getTxStockCode());
        salesOrderAtomPageQueryRequest.setReceiveFactoryCode(request.getTxFactoryCode());
        salesOrderAtomPageQueryRequest.setCreatorCode(request.getCreatorCode());
        salesOrderAtomPageQueryRequest.setModifierCode(request.getModifierCode());
        salesOrderAtomPageQueryRequest.setCreateBeginTime(request.getCreateBeginTime());
        salesOrderAtomPageQueryRequest.setCreateEndTime(request.getCreateEndTime());
        salesOrderAtomPageQueryRequest.setModifyBeginTime(request.getModifyBeginTime());
        salesOrderAtomPageQueryRequest.setModifyEndTime(request.getModifyEndTime());
        salesOrderAtomPageQueryRequest.setCustomerPurchaseOrderNo(request.getCustomerPurchaseOrderNo());
        salesOrderAtomPageQueryRequest.setSubStatusList(request.getSubStatusList());
        salesOrderAtomPageQueryRequest.setNote(request.getNote());
        salesOrderAtomPageQueryRequest.setProductCodeList(request.getProductCodeList());
        salesOrderAtomPageQueryRequest.setCloseReasonCodeList(request.getCloseReasonCodeList());
        salesOrderAtomPageQueryRequest.setOrderTag(SetOrderTag.RETURN);

        return salesOrderAtomPageQueryRequest;
    }

    public static List<ReturnOrderGroupPageQueryResponse> convertPageQueryResponse(Result<List<ObtcSaleOrderIndex>> listResult) {
        if(Objects.isNull(listResult) || !listResult.isSuccess() || CollectionUtils.isEmpty(listResult.getResult())) {
            return Lists.newArrayList();
        }
        return listResult.getResult().stream()
                .map(AtomOrderDOToReturnOrderAtomQueryResponseConvertor::convertMainOrder)
                .collect(Collectors.toList());
    }

    private static ReturnOrderGroupPageQueryResponse convertMainOrder(ObtcSaleOrderIndex order) {
        ReturnOrderGroupPageQueryResponse response = new ReturnOrderGroupPageQueryResponse();
        response.setFromSys(nvlToEmptyStr(order.getMainFromSys()));
        if (Objects.nonNull(order.getMainBusinessType())) {
            response.setBusinessType(String.valueOf(order.getMainBusinessType()));
        }else {
            response.setBusinessType("");
        }
        response.setBusinessTypeName(nvlToEmptyStr(order.getMainBusinessTypeName()));
        response.setBusinessUnitCode(nvlToEmptyStr(order.getMainBisUnitOutCode()));
        response.setBusinessUnitName(nvlToEmptyStr(order.getMainBisUnitName()));
        response.setOrderCategoryCode(nvlToEmptyStr(order.getMainOrderCategoryCode()));
        if (StringUtils.isNotBlank(response.getOrderCategoryCode())) {
            OrderCategoryEnum orderCategoryEnum = OrderCategoryEnum.of(order.getMainOrderCategoryCode());
            if (orderCategoryEnum != null) {
                response.setOrderCategoryName(orderCategoryEnum.getDesc());
            } else {
                response.setOrderCategoryName("");
            }
        }
        response.setSalesOrderNo(nvlToEmptyStr(order.getMainOrderBizId()));
        response.setSourceOrderNo(nvlToEmptyStr(order.getMainSourceOrderNo()));
        response.setOutOrderNo(nvlToEmptyStr(order.getMainOuterOrderId()));
        response.setOriginalOrderNo(nvlToEmptyStr(order.getMainOriginalOrderNo()));
        response.setProductGroupCode(nvlToEmptyStr(order.getMainProductGroupCode()));
        response.setProductGroupName(nvlToEmptyStr(order.getMainProductGroupName()));
        response.setBusinessChannel(nvlToEmptyStr(order.getMainChannelOutCode()));
        response.setBusinessChannelName(nvlToEmptyStr(order.getMainChannelName()));
        response.setSalesOrganizationCode(nvlToEmptyStr(order.getMainOrganizationOutCode()));
        response.setSalesOrganizationName(nvlToEmptyStr(order.getMainOrganizationName()));
        response.setCustomerCode(nvlToEmptyStr(order.getMainCustomerCode()));
        response.setCustomerName(nvlToEmptyStr(order.getMainCustomerName()));
        response.setShipToPartyCode(nvlToEmptyStr(order.getMainReceiveAddressCode()));
        response.setShipToPartyName(nvlToEmptyStr(order.getMainReceiveDetailAddress()));
        response.setSalesChannelCode(nvlToEmptyStr(order.getMainSaleChannelOutCode()));
        response.setSalesChannelName(nvlToEmptyStr(order.getMainSaleChannelName()));
        response.setSalesDepartmentCode(nvlToEmptyStr(order.getMainDepartmentOutCode()));
        response.setSalesDepartmentName(nvlToEmptyStr(order.getMainDepartmentName()));
        response.setCustomerGrade(nvlToEmptyStr(order.getMainCustomerGrade()));
        response.setCustomerGroup(nvlToEmptyStr(order.getMainCustomerGroup()));
        response.setSalesOrderType(nvlToEmptyStr(order.getMainOrderType()));
        response.setSalesOrderTypeName(nvlToEmptyStr(order.getMainOrderTypeName()));
        response.setDeliveryMethodCode(nvlToEmptyStr(order.getMainTransportModeCode()));
        response.setDeliveryMethodName(nvlToEmptyStr(order.getMainTransportModeName()));
        response.setExpectedDeliveryDate(DateUtils.toDateStr(order.getMainCustomerExpectDate(), DateUtils.DATE_FORMAT));
        response.setFulfillmentTypeCode(nvlToEmptyStr(order.getMainFulfillmentTypeCode()));
        response.setFulfillmentPriority(nvlToEmptyStr(order.getMainFulfillmentPriority()));
        response.setAutoFulfillment(nvlToEmptyStr(order.getMainAutoFulfillment()));
        if(org.apache.commons.lang3.StringUtils.isNotBlank(order.getMainOrderBizTag())) {
            String[] orderBizTagArray = order.getMainOrderBizTag().split(",");
            response.setOrderBizTag(Arrays.asList(orderBizTagArray));
        }else{
            response.setOrderBizTag(Lists.newArrayList());
        }
        response.setOrderReasonCode(nvlToEmptyStr(order.getMainCreateReason()));
        response.setOrderReasonName(nvlToEmptyStr(order.getMainCreateReasonName()));
        response.setShippingWarehouseCode(nvlToEmptyStr(order.getMainWareHouseOutCode()));
        response.setShippingWarehouseName(nvlToEmptyStr(order.getMainWareHouseName()));
        response.setDeliveryFactoryCode(nvlToEmptyStr(order.getMainDeliveryFactoryCode()));
        response.setDeliveryFactoryName(nvlToEmptyStr(order.getMainDeliveryFactoryName()));
        response.setCreateTime(DateUtils.toDateStr(order.getMainGmtCreate(), DateUtils.DATETIME_FORMAT));
        response.setCreatorCode(nvlToEmptyStr(order.getMainCreatorCode()));
        response.setCreatorName(nvlToEmptyStr(order.getMainCreatorName2()));
        response.setModifierCode(nvlToEmptyStr(order.getMainModifierCode()));
        response.setModifierName(nvlToEmptyStr(order.getMainModifierName2()));
        response.setModifierTime(DateUtils.toDateStr(order.getMainGmtModified(), DateUtils.DATETIME_FORMAT));
        response.setCustomerPurchaseDate(nvlToEmptyStr(DateUtils.toDateStr(order.getMainCustomerPurchaseDate(), DateUtils.DATE_FORMAT)));
        response.setCustomerPurchaseOrderNo(nvlToEmptyStr(order.getMainCustomerReferenceNo()));
        response.setNote(nvlToEmptyStr(order.getMainRemark()));
        if(order.getMainStatus() != null) {
            response.setStatus(String.valueOf(order.getMainStatus()));
        }else {
            response.setStatus("");
        }
        response.setSubStatus(nvlToEmptyStr(order.getMainSubStatus()));
        response.setBizFeatures(nvlToEmptyStr(order.getMainBizFeatures()));
        response.setStockCode(nvlToEmptyStr(order.getMainStockCode()));
        response.setStockName(nvlToEmptyStr(order.getMainStockName()));
        response.setCustomerGroupName(nvlToEmptyStr(order.getMainCustomerGroupName()));
        response.setCostCenter(nvlToEmptyStr(order.getMainCostCenter()));
        response.setOrderChannelCode(nvlToEmptyStr(order.getMainOrderChannelCode()));

        if (StringUtils.isNotBlank(response.getOrderChannelCode())) {
            OrderChannelEnum orderChannelEnum = OrderChannelEnum.of(response.getOrderChannelCode());
            if (Objects.nonNull(orderChannelEnum)){
                response.setOrderChannelName(nvlToEmptyStr(orderChannelEnum.getDesc()));
            }else {
                response.setOrderChannelName("");
            }
        }

        response.setTxWarehouseCode(nvlToEmptyStr(order.getMainReceiveWareHouseOutCode()));
        response.setTxWarehouseName(nvlToEmptyStr(order.getMainReceiveWarehouseName()));
        response.setTxFactoryCode(nvlToEmptyStr(order.getMainReceiveFactoryCode()));
        response.setTxFactoryName(nvlToEmptyStr(order.getMainReceiveFactoryName()));
        response.setTxStockCode(nvlToEmptyStr(order.getMainReceiveStockCode()));
        response.setTxStockName(nvlToEmptyStr(order.getMainReceiveStockName()));

        return response;
    }

    /**
     * 字段值如果为空，返回空字符串，而不是null。
     *
     * @param source
     * @return
     */
    private static String nvlToEmptyStr(String source) {
        if (org.apache.commons.lang.StringUtils.isBlank(source)) {
            return "";
        }
        return source;
    }

    public static PageQuerySaleOrderDetailGroupRequest convertDetailPageQueryRequest(ReturnOrderGroupDetailPageQueryRequest request) {
        PageQuerySaleOrderDetailGroupRequest esRequest = new PageQuerySaleOrderDetailGroupRequest();

        esRequest.setFromSysList(request.getFromSysList());
        esRequest.setBusinessType(request.getBusinessType());
        esRequest.setBisUnitCodeList(request.getBusinessUnitCodeList());
        esRequest.setStart((request.getPage() - 1) * request.getSize());
        esRequest.setLimit(request.getSize());
        esRequest.setOrderChannelCodeList(request.getOrderChannelCodeList());
        esRequest.setBizCodeList(request.getSalesOrderNoList());
        esRequest.setOriginalOrderNoList(request.getOriginalOrderNoList());
        esRequest.setSourceOrderNoList(request.getSourceOrderNoList());
        esRequest.setOutOrderNoList(request.getOutOrderNoList());
        esRequest.setProductGroupCode(request.getProductGroupCode());
        esRequest.setChannelCode(request.getBusinessChannelCode());
        esRequest.setOrganizationCode(request.getSalesOrganizationCode());
        esRequest.setCustomerCode(request.getCustomerCode());
        esRequest.setReceiveAddressCode(request.getShipToPartyCode());
        esRequest.setSaleChannelCodeList(request.getSalesChannelCodeList());
        esRequest.setDepartmentCodeList(request.getSalesDepartmentCodeList());
        esRequest.setCustomerGroup(request.getCustomerGroup());
        esRequest.setOrderTypeCodeList(request.getSalesOrderTypeList());
        esRequest.setStatusList(request.getOrderStatusList());
        esRequest.setDeliveryMethodCodeList(request.getDeliveryMethodCodeList());
        esRequest.setCustomerExpectBeginDate(DateUtils.toMillisecondStr(request.getExpectedDeliveryBeginDate(), Boolean.TRUE));
        esRequest.setCustomerExpectEndDate(DateUtils.toMillisecondStr(request.getExpectedDeliveryEndDate(), Boolean.FALSE));
        esRequest.setOrderReasonCodeList(request.getOrderReasonCodeList());
        esRequest.setReceiveWarehouseCodeList(request.getTxWarehouseCodeList());
        esRequest.setShippingWarehouseCodeList(request.getShippingWarehouseCodeList());
        esRequest.setReceiveFactoryCode(request.getTxFactoryCode());
        esRequest.setReceiveStockCode(request.getTxStockCode());
        esRequest.setCreatorCode(request.getCreatorCode());
        esRequest.setModifierCode(request.getModifierCode());
        esRequest.setGmtBeginCreate(DateUtils.dateToStamp(request.getCreateBeginTime()));
        esRequest.setGmtEndCreate(DateUtils.dateToStamp(request.getCreateEndTime()));
        esRequest.setGmtBeginModified(DateUtils.dateToStamp(request.getModifyBeginTime()));
        esRequest.setGmtEndModified(DateUtils.dateToStamp(request.getModifyEndTime()));
        esRequest.setCustomerPurchaseOrderNo(request.getCustomerPurchaseOrderNo());
        esRequest.setSubSubStatusList(request.getSubStatusList());
        esRequest.setRemark(request.getNote());
        esRequest.setScItemIdList(request.getProductCodeList());
        esRequest.setClosed(request.getIsClose());
        esRequest.setRefuseReasonCodeList(request.getCloseReasonCodeList());
        esRequest.setSubRemark(request.getDetailNote());

        return esRequest;
    }

    public static List<ReturnOrderGroupDetailPageQueryResponse> convertDetailPageQueryResponse(Result<List<ObtcSaleOrderLineIndex>> listResult) {
        List<ReturnOrderGroupDetailPageQueryResponse> responsesList = new ArrayList<>();
        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return responsesList;
        }

        for (ObtcSaleOrderLineIndex orderLineIndex : listResult.getResult()) {
            ReturnOrderGroupDetailPageQueryResponse response = new ReturnOrderGroupDetailPageQueryResponse();

            response.setFromSys(nvlToEmptyStr(orderLineIndex.getMainFromSys()));
            response.setBusinessType(nvlToEmptyStr(String.valueOf(orderLineIndex.getMainBusinessType())));
            response.setBusinessTypeName(nvlToEmptyStr(orderLineIndex.getMainBusinessTypeName()));
            response.setBusinessUnitCode(nvlToEmptyStr(orderLineIndex.getMainBisUnitOutCode()));
            response.setBusinessUnitName(nvlToEmptyStr(orderLineIndex.getMainBisUnitName()));
            response.setOrderCategoryCode(nvlToEmptyStr(orderLineIndex.getMainOrderCategoryCode()));
            response.setOrderChannelCode(nvlToEmptyStr(orderLineIndex.getMainOrderChannelCode()));
            response.setSalesOrderNo(nvlToEmptyStr(orderLineIndex.getMainOrderBizId()));
            response.setSourceOrderNo(nvlToEmptyStr(orderLineIndex.getMainSourceOrderNo()));
            response.setOutOrderNo(nvlToEmptyStr(orderLineIndex.getMainOuterOrderId()));
            response.setOriginalOrderNo(nvlToEmptyStr(orderLineIndex.getMainOriginalOrderNo()));
            response.setProductGroupCode(nvlToEmptyStr(orderLineIndex.getMainProductGroupCode()));
            response.setProductGroupName(nvlToEmptyStr(orderLineIndex.getMainProductGroupName()));
            response.setBusinessChannel(nvlToEmptyStr(orderLineIndex.getMainChannelCode()));
            response.setBusinessChannelName(nvlToEmptyStr(orderLineIndex.getMainChannelName()));
            response.setSalesOrganizationCode(nvlToEmptyStr(orderLineIndex.getMainOrganizationOutCode()));
            response.setSalesOrganizationName(nvlToEmptyStr(orderLineIndex.getMainOrganizationName()));
            response.setCustomerCode(nvlToEmptyStr(orderLineIndex.getMainCustomerCode()));
            response.setCustomerName(nvlToEmptyStr(orderLineIndex.getMainCustomerName()));
            response.setShipToPartyCode(nvlToEmptyStr(orderLineIndex.getMainReceiveAddressCode()));
            response.setShipToPartyName(nvlToEmptyStr(orderLineIndex.getMainReceiveDetailAddress()));
            response.setSalesChannelCode(nvlToEmptyStr(orderLineIndex.getMainSaleChannelOutCode()));
            response.setSalesChannelName(nvlToEmptyStr(orderLineIndex.getMainSaleChannelName()));
            response.setSalesDepartmentCode(nvlToEmptyStr(orderLineIndex.getMainDepartmentOutCode()));
            response.setSalesDepartmentName(nvlToEmptyStr(orderLineIndex.getMainDepartmentName()));
            response.setCustomerGrade(nvlToEmptyStr(orderLineIndex.getMainCustomerGrade()));
            response.setCustomerGroup(nvlToEmptyStr(orderLineIndex.getMainCustomerGroup()));
            response.setCustomerGroupName(nvlToEmptyStr(orderLineIndex.getMainCustomerGroupName()));
            response.setSalesOrderType(nvlToEmptyStr(orderLineIndex.getMainOrderType()));
            response.setSalesOrderTypeName(nvlToEmptyStr(orderLineIndex.getMainOrderTypeName()));
            response.setDeliveryMethodCode(nvlToEmptyStr(orderLineIndex.getMainTransportModeCode()));
            response.setDeliveryMethodName(nvlToEmptyStr(orderLineIndex.getMainTransportModeName()));
            response.setExpectedDeliveryDate(nvlToEmptyStr(DateUtils.toDateStr(orderLineIndex.getMainCustomerExpectDate(),DateUtils.DATE_FORMAT)));
            response.setCostCenter(nvlToEmptyStr(orderLineIndex.getMainCostCenter()));
            response.setFulfillmentTypeCode(nvlToEmptyStr(orderLineIndex.getMainFulfillmentTypeCode()));
            response.setAutoFulfillment(nvlToEmptyStr(orderLineIndex.getMainAutoFulfillment()));
            response.setFulfillmentPriority(nvlToEmptyStr(orderLineIndex.getMainFulfillmentPriority()));
            response.setOrderBizTag(orderLineIndex.getMainOrderBizTag() == null ? Lists.newArrayList() : Lists.newArrayList(orderLineIndex.getMainOrderBizTag().split(",")));
            response.setOrderReasonCode(nvlToEmptyStr(orderLineIndex.getMainCreateReason()));
            response.setOrderReasonName(nvlToEmptyStr(orderLineIndex.getMainCreateReasonName()));
            response.setCreatorCode(nvlToEmptyStr(orderLineIndex.getMainCreatorCode()));
            response.setCreatorName(nvlToEmptyStr(orderLineIndex.getMainCreatorName()));
            response.setCreateTime(nvlToEmptyStr(DateUtils.toDateStr(orderLineIndex.getMainGmtCreate(), DateUtils.DATETIME_FORMAT)));
            response.setModifierCode(nvlToEmptyStr(orderLineIndex.getMainModifierCode()));
            response.setModifierName(nvlToEmptyStr(orderLineIndex.getMainModifierName2()));
            response.setModifierTime(nvlToEmptyStr(DateUtils.toDateStr(orderLineIndex.getMainGmtModified(), DateUtils.DATETIME_FORMAT)));
            response.setCustomerPurchaseOrderNo(nvlToEmptyStr(orderLineIndex.getMainCustomerReferenceNo()));
            response.setCustomerPurchaseDate(nvlToEmptyStr(DateUtils.toDateStr(orderLineIndex.getMainCustomerPurchaseDate(),DateUtils.DATE_FORMAT)));
            response.setNote(nvlToEmptyStr(orderLineIndex.getMainRemark()));
            response.setStatus(nvlToEmptyStr(String.valueOf(orderLineIndex.getMainStatus())));
            response.setSubStatus(nvlToEmptyStr(orderLineIndex.getMainSubStatus()));
            response.setBizFeatures(nvlToEmptyStr(orderLineIndex.getMainBizFeatures()));
            response.setLineNum(nvlToEmptyStr(orderLineIndex.getSubSerialNumber()));
            response.setSourceLineNum(nvlToEmptyStr(orderLineIndex.getSubSourceLineNum()));
            response.setOutLineNum(nvlToEmptyStr(orderLineIndex.getSubOuterOrderLineId()));
            response.setInnerProductCode(nvlToEmptyStr(orderLineIndex.getSubInnerProductCode()));
            response.setInnerProductName(nvlToEmptyStr(orderLineIndex.getSubInnerProductName()));
            response.setProductBarCode(nvlToEmptyStr(orderLineIndex.getSubProductBarCode()));
            response.setProductCode(nvlToEmptyStr(orderLineIndex.getSubScItemOutCode()));
            response.setProductName(nvlToEmptyStr(orderLineIndex.getSubScItemTitle()));
            response.setSalesUnitCode(nvlToEmptyStr(orderLineIndex.getSubSaleUnit()));
            response.setSalesUnitName(nvlToEmptyStr(orderLineIndex.getSubSaleUnitName()));
            response.setStockUnitCode(nvlToEmptyStr(orderLineIndex.getSubInventoryUnitCode()));
            response.setStockUnitName(nvlToEmptyStr(orderLineIndex.getSubInventoryUnit()));
            response.setUnitConversionRelationship(nvlToEmptyStr(orderLineIndex.getSubUnitConvert()));
            response.setTxWarehouseCode(nvlToEmptyStr(orderLineIndex.getSubReceiveWarehouseOutCode()));
            response.setTxWarehouseName(nvlToEmptyStr(orderLineIndex.getSubReceiveWarehouseName()));
            response.setTxFactoryCode(nvlToEmptyStr(orderLineIndex.getSubReceiveFactoryCode()));
            response.setTxFactoryName(nvlToEmptyStr(orderLineIndex.getMainReceiveFactoryName()));
            response.setTxStockCode(nvlToEmptyStr(orderLineIndex.getSubReceiveStockCode()));
            response.setTxStockName(nvlToEmptyStr(orderLineIndex.getSubReceiveStockName()));
            response.setShippingWarehouseCode(nvlToEmptyStr(orderLineIndex.getSubShippingWarehouseOutCode()));
            response.setShippingWarehouseName(nvlToEmptyStr(orderLineIndex.getSubShippingWarehouseName()));
            response.setDeliveryFactoryCode(nvlToEmptyStr(orderLineIndex.getSubDeliveryFactoryCode()));
            response.setDeliveryFactoryName(nvlToEmptyStr(orderLineIndex.getSubDeliveryFactoryName()));
            response.setStockCode(nvlToEmptyStr(orderLineIndex.getSubStockCode()));
            response.setStockName(nvlToEmptyStr(orderLineIndex.getSubStockName()));
            response.setUnitPrice(Objects.isNull(orderLineIndex.getSubUnitPrice()) ? "" : MoneyUtils.moneyFen2Yuan(Long.valueOf(nvlToEmptyStr(orderLineIndex.getSubUnitPrice()))));
            response.setTotalAmount(Objects.isNull(orderLineIndex.getSubShouldPayFee()) ? "" : MoneyUtils.moneyFen2Yuan(Long.valueOf(nvlToEmptyStr(orderLineIndex.getSubShouldPayFee()))));
            response.setProductionDate(nvlToEmptyStr(DateUtils.toDateStr(orderLineIndex.getSubProductationTime(), DateUtils.PATTEN_YMD)));
            response.setProductQuantity(nvlToEmptyStr(String.valueOf(orderLineIndex.getSubQuantity())));
            response.setCumulativeNoticeQuantity(nvlToEmptyStr(orderLineIndex.getSubCumulativeNoticeQuantity()));
            response.setCumulativePlanDeliveryQuantity(nvlToEmptyStr(orderLineIndex.getSubCumulativeDeliveryQuantity()));
            response.setCumulativeReceivedQuantity(nvlToEmptyStr(orderLineIndex.getSubCumulativeReceivedQuantity()));
            response.setCumulativeWaitReceivedQuantity(nvlToEmptyStr(orderLineIndex.getSubCumulativeWaitOutboundQuantity()));
            response.setCumulativeDeliveryQuantity(nvlToEmptyStr(orderLineIndex.getSubDeliveryAmount()));
            response.setIsClose(nvlToEmptyStr(String.valueOf(orderLineIndex.getSubClosed())));
            response.setCloseCode(nvlToEmptyStr(orderLineIndex.getSubRefuseReasonCode()));
            response.setCloseMsg(nvlToEmptyStr(orderLineIndex.getSubRefuseReasonName()));
            response.setDetailNote(nvlToEmptyStr(orderLineIndex.getSubRemark()));
            response.setDetailStatus(nvlToEmptyStr(String.valueOf(orderLineIndex.getSubStatus())));
            response.setDetailBizFeatures(nvlToEmptyStr(orderLineIndex.getSubBizFeatures()));

            if (StringUtils.isNotBlank(orderLineIndex.getSubFeatures())){
                Map<String, String> subFeatures = JSONObject.parseObject(orderLineIndex.getSubFeatures(), Map.class);
                if (MapUtils.isNotEmpty(subFeatures)){
                    response.setInvoiceNo(subFeatures.get(OrderFeaturesKey.INVOICE_NO) == null ? "" : nvlToEmptyStr(subFeatures.get(OrderFeaturesKey.INVOICE_NO)));
                    response.setInvoiceLineNo(subFeatures.get(OrderFeaturesKey.INVOICE_LINE_NO) == null ? "" : nvlToEmptyStr(subFeatures.get(OrderFeaturesKey.INVOICE_LINE_NO)));
                }
            }else {
                response.setInvoiceNo("");
                response.setInvoiceLineNo("");
            }

            responsesList.add(response);
        }

        return responsesList;
    }
}
